<?php

namespace app\api\controller;

use app\admin\model\Anli as ModelAnli;
use app\common\controller\Api;
use think\Db;

class Anli extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    /**
     * 首页实景案例列表
     */
    public function getAnliIndexList()
    {
        $list = ModelAnli::where('status', 1)->order('weigh', 'desc')->field('id,name,image,type_ids,content')->limit(5)->select();
        foreach ($list as $key => $value) {
            $list[$key]['image'] = cdnurl($value['image'], true);
            $typeIds = explode(',', $value['type_ids']);
            $res = Db::name('typetwo')->where('id', 'in', $typeIds)->field('id,name,image')->select();
            foreach ($res as $k => $v) {
                $res[$k]['image'] = cdnurl($v['image'], true);
            }
            $list[$key]['type_detail'] = $res;
        }
        $this->success('获取成功', $list);
    }
    /**
     * 实景案例列表
     */
    public function getAnliList()
    {
        $page = $this->request->param('page/d', 1);
        $typeId = $this->request->param('type_id/d', 0);
        if ($typeId) {
            $where = ['status' => 1, 'type_id' => $typeId];
        } else {
            $where = ['status' => 1];
        }
        $list = ModelAnli::where($where)
            ->order('weigh', 'desc')
            ->field('id,name,image,browse_num')
            ->paginate(['list_rows' => 10, 'page' => $page])
            ->each(function ($item, $key) {
                $item['image'] = cdnurl($item['image'], true);
            });
        $this->success('获取成功', $list);
    }
    /**
     * 案例详情
     */
    public function getAnliDetail()
    {
        $id = $this->request->param('id/d', 0);
        if (!$id) {
            $this->error('未传必填参数!');
        }
        $info = ModelAnli::where('id', $id)->find();
        if (!$info) {
            $this->error('案例不存在');
        }
        $info['images'] = explode(',', $info['images']);
        $info['images'] = array_map(function ($item) {
            return cdnurl($item, true);
        }, $info['images']);
        $info['image'] = cdnurl($info['image'], true);
        if ($info['videofile']) {
            $info['video_file'] = cdnurl($info['videofile'], true);
        }
        $info['createtime'] = date('Y-m-d H:i:s', $info['createtime']);
        $info['anli_content'] = getHandleRichtext($info['anli_content']);
        unset($info['status']);
        unset($info['weigh']);
        unset($info['updatetime']);
        // 浏览量+1
        ModelAnli::where('id', $id)->inc('browse_num')->update();
        $this->success('获取成功', $info);
    }
    /**
     * 案例分类列表
     *  */
    public function getAnliTypeList()
    {
        $list = Db::name('anlitype')->where('status', 1)->field('id,name,image')->order("weigh desc")->limit(10)->select();
        // 列表头部加一个全部
        $all = ['id' => 0, 'name' => '全部', 'image' => ''];
        array_unshift($list, $all);
        foreach ($list as $key => $value) {
            if ($value['image']) {
                $list[$key]['image'] = cdnurl($value['image'], true);
            }
        }
        $this->success('获取成功', $list);
    }
}
